import {
  delThemeCategory,
  themeCategoryDetail,
  themeCategoryList
} from "@/api/service/theme/theme";
import { form, resetForm, showForm } from "./form";
import { keys, query, queryObj, table } from "./table";

export const list = async () => {
  table.value.loading = true;
  const data = await themeCategoryList(query.value);
  table.value.data = data.rows.map((v: any) => {
    delete v.children;
    return v;
  });
  table.value.loading = false;
  query.value.total = data.total;
};

export const edit = async (id?: string) => {
  const targetId = id || keys.value[0];
  const data = await themeCategoryDetail(targetId);
  form.value = data.data;
  showForm.value = true;
};
export const create = async () => {
  resetForm();
  showForm.value = true;
};
export const del = async (id?: string) => {
  const targetId = id || keys.value;
  const ids = typeof targetId === "string" ? targetId : targetId.join(",");
  await delThemeCategory(ids);
  await list();
  keys.value = [];
};

export const search = () => {
  query.value.pageNum = 1;
  list();
};

export const resetQuery = () => {
  // @ts-ignore
  query.value = useQuery({
    ...queryObj
  }).value;
};
